<template>
	<b-submit-page
		submitText="新增收货地址"
		:extraClass="!loading && data.length === 0 ? 'h-full flex-center' : ''"
		@submit="toAdd"
		buttonIcon='icon-a-zu491'
	>
		<view class="p-2-2">
			<c-skeleton-list :data="data" :loading="loading">
				<ContactItem v-for="(el, i) in data" :key="el.id" :data="el" :mb="i < data.length - 1" @touch="handleItem(el)" @touchIcon="toEdit(el)" />
			</c-skeleton-list>
		</view>
	</b-submit-page>
</template>

<script>
import { onMounted, ref } from 'vue';
import { listContactAddresses } from '@/base/api/contact.js';
import ContactItem from "./contact-item.vue";
export default {
	components: {
		ContactItem
	},
	onLoad(){
		getApp().globalData.resetCurPageUpdate();
		const c = this.getOpenerEventChannel();
		c.on('data', (data) => {
			if (data) {
				this.reloadOrderContact = data.reloadContact;
			}
		});
	},
	async onShow(){
		if(getApp().globalData.curPageNeedUpdate()){
			await this.reload(true);
			if(this.reloadOrderContact) {
				this.reloadOrderContact();
			}
		}
	},
	setup() {
		const data = ref([]);
		const reloadOrderContact = ref();
		const loading = ref(true);
		async function reload(globalLoading) {
			!globalLoading && (loading.value = true);
			const newData = await listContactAddresses(globalLoading);
			loading.value = false;
			if (newData) {
				data.value = newData;
			}
		}
		onMounted(() => {
			reload(false);
		});
		function toAdd(){
			uni.navigateTo({
				url: "/pages/contact-cu/contact-cu?create=1"
			});
		}
		function handleItem(item){
			if(reloadOrderContact.value) {
				reloadOrderContact.value(item);
				uni.navigateBack();
			} else {
				toEdit(item);
			}
		}
		function toEdit(item){
			uni.navigateTo({
				url: "/pages/contact-cu/contact-cu",
				success(e){
					e.eventChannel.emit('data', {
						detail: item
					});
				}
			});
		}
		return {
			data,
			loading,
			toAdd,
			toEdit,
			reload,
			handleItem,
			reloadOrderContact,
		};
	},
};
</script>

<style lang="scss">
</style>
